<?php
/*
*  -------------------------------------------------
*   @file		: .php
*   @link		:CPC  www.chinapet.com
*   @copyright	: 2002-2012 Xingmo Inc
*   @author		: Laipiyang <462166282@qq.com>
*   @blog_url   : http://www.icuir.com/
*   @blog_url   : http://blog.sina.com.cn/xmphp
*   @date		:
*   @update		:
*  -------------------------------------------------
*/
include_once './include/init.inc.php';
$filename=basename(__FILE__);
checklogin();
$act=isset ( $_GET['act'] ) ? trim ( $_GET['act'] ) : '' ;
if($act=='add')
{
	$smarty->assign('web',array('title'=>'添加分类类型','act'=>'insert','btn'=>'添加类型完成'));
	$smarty->display(ADMIN_TPL.'usual_sort_info.dwt');

}
else if ($act=='insert')
{
/*
     [sort_name] => rere
    [is_enable] => 1
    [type] => 3
    [site_type] => 2
    [link_url] => rer
    [sort_order] => re
    [sort_id] =>
*/

	$sort_name=isset ( $_POST['sort_name'] ) ? trim ( $_POST['sort_name'] ) : '' ;
	$is_enable=isset ( $_POST['is_enable'] ) ? intval ( $_POST['is_enable'] ) : 0 ;
	$type=isset ( $_POST['type'] ) ? intval ( $_POST['type'] ) :0 ;
	$site_type=isset ( $_POST['site_type'] ) ? intval ( $_POST['site_type'] ) : '' ;
	$link_url=isset ( $_POST['link_url'] ) ? trim ( $_POST['link_url'] ) : '' ;
	$sort_order=isset ( $_POST['sort_order'] ) ? intval ( $_POST['sort_order'] ) : '' ;
	$dateline=time();

	if(strlen($sort_name)<4 || strlen($sort_name)>100 )
	{
		alert('分类名称不能小于两个字符');
		exit();
	}
	if($db->fetch_one("SELECT * FROM `info_q_usual_sort` WHERE `sort_name`='{$sort_name}'"))
	{
		  alert('该分类已经存在,请重新输入');
		  exit();
	}
	if(!isset($is_enable))
	{
		$is_enable=0;
	}
	if(empty($type) || !isset($type))
	{
		alert('对不起，分类类型必选...');
		exit();
	}
	if(empty($site_type))
	{
		 $site_type=1;
	}
	if($link_url)
	{
		  if(!preg_match('/^http:\/\/((([a-zA-Z0-9])|([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))\.)+[a-zA-Z]{2,4}.*$/',$link_url))
		  {
			  alert('邮箱地址不合法,再说你填的也不对啊...');
			  exit();
		  }
	}

	//构造sql
	$sql="INSERT INTO `info_q_usual_sort` (`sort_name`,`is_enable`,`type`,`site_type`,`link_url`,`sort_order`,`dateline`) VALUES ('{$sort_name}','{$is_enable}','{$type}','{$site_type}','{$link_url}','{$sort_order}','{$dateline}') ";
     if($db->query($sql))
     {
		alert('分类添加成功',$filename);
		exit();
     }
	 else
	 {
		alert('分类添加失败');
		exit();
	 }
}
else if ($act=='edit')
{
      $sort_id=isset ( $_GET['sort_id'] ) ? intval ( $_GET['sort_id'] ) :0 ;
	  if($sort_id<1)
	  {
		  alert('非法入口',$filename);
		  exit();
	  }
	  $sql="SELECT * FROM `info_q_usual_sort` WHERE `sort_id`='{$sort_id}' ";
	  $row=$db->fetch_one($sql);
	  if(!$row)
	  {
		 alert('分类数据不存在');
		 exit();
	  }
	  $smarty->assign('row',$row);
	  $smarty->assign('web',array('act'=>'update','title'=>'修改分类','btn'=>'分类修改完成'));
	  $smarty->display(ADMIN_TPL.'usual_sort_info.dwt');

}
else if ($act=='update')
{
	$sort_id=isset ( $_POST['sort_id'] ) ? intval ( $_POST['sort_id'] ) : 0;
	if($sort_id<1)
	{
		exit(alert('操作非法'));
	}
	$sort_name=isset ( $_POST['sort_name'] ) ? trim ( $_POST['sort_name'] ) : '' ;
	$is_enable=isset ( $_POST['is_enable'] ) ? intval ( $_POST['is_enable'] ) : 0 ;
	$type=isset ( $_POST['type'] ) ? intval ( $_POST['type'] ) :0 ;
	$site_type=isset ( $_POST['site_type'] ) ? intval ( $_POST['site_type'] ) : '' ;
	$link_url=isset ( $_POST['link_url'] ) ? trim ( $_POST['link_url'] ) : '' ;
	$sort_order=isset ( $_POST['sort_order'] ) ? intval ( $_POST['sort_order'] ) : '' ;
	$dateline=time();

	if(strlen($sort_name)<4 || strlen($sort_name)>100 )
	{
		alert('分类名称不能小于两个字符');
		exit();
	}
	if($db->fetch_one("SELECT * FROM `info_q_usual_sort` WHERE `sort_name`='{$sort_name}' AND `sort_id`<>'{$sort_id}'"))
	{
		  alert('该分类所属分类已经存在,请重新输入输入');
		  exit();
	}
	if(!isset($is_enable))
	{
		$is_enable=0;
	}
	if(empty($type) || !isset($type))
	{
		alert('对不起，分类类型必选...');
		exit();
	}
	if(empty($site_type))
	{
		 $site_type=1;
	}
	if($link_url)
	{
		  if(!preg_match('/^http:\/\/((([a-zA-Z0-9])|([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]))\.)+[a-zA-Z]{2,4}.*$/',$link_url))
		  {
			  alert('邮箱地址不合法,再说你填的也不对啊...');
			  exit();
		  }
	}

	//sql update
	 $sql="UPDATE `info_q_usual_sort` SET `sort_name`='{$sort_name}',`is_enable`='{$is_enable}',`type`='{$type}',`site_type`='{$site_type}',`link_url`='{$link_url}',`sort_order`='{$sort_order}',`dateline`='{$dateline}' WHERE `sort_id`='{$sort_id}'";
	if($db->query($sql))
	{
		 alert('分类修改完成',$filename);
		 exit();
	}
	else
	{
		alert('分类修改失败',$filename);
		exit();
	}
}
else if ($act=='del')
{
	 $sort_id=isset ( $_GET['sort_id'] ) ? intval ( $_GET['sort_id'] ) : 0 ;
	 if($sort_id<1)
	 {
		 alert('操作非法');
		 exit();
	 }
	 $sql="DELETE FROM `info_q_usual_sort` WHERE `sort_id`='{$sort_id}'";
	 $res=$db->query($sql);
	 if($res)
	 {
		 alert('分类删除成功',$filename);
		 exit();
	 }
	 else
	 {
		alert('分类删除失败',$filename);
	 }

}
else if ($act=='ajax')
{
     $sort_id=isset ( $_POST['sort_id'] ) ? intval ( $_POST['sort_id'] ) : '';
	 if($sort_id=='')
	 {
		echo -1;
		exit;
	 }
	 $sql="SELECT * FROM `info_q_usual_sort` WHERE `sort_id`='{$sort_id}' ";
	 $query=$db->fetch_one($sql);
	 if(!$query)
	 {
		echo -2;
		exit;
	 }
	 echo $s=$query['is_enable'] ? 0 : 1 ;   //取出is_enble的值如果为1 表示图片为对号  变成叉 为0直接编程1 为对号
	 $sql="UPDATE `info_q_usual_sort` SET `is_enable`='{$s}' WHERE `sort_id`='{$sort_id}' ";
	 $db->query($sql);
}
else
{

     $smarty->assign('pl',$pl=licuicui_page($db->num_rows($db->query("SELECT * FROM `info_q_usual_sort`")),$filename,10));
	 $sort=$db->fetch_all("SELECT * FROM `info_q_usual_sort`  ORDER BY `type` DESC  LIMIT {$pl['start']},{$pl['pagesize']}");
	 foreach ($sort as $key => $val )
	 {
        if($val['type'])
        {
			 if($val['type']=='1')
			 {
				$sort[$key]['type1']='勋章';
			 }else if ($val['type']=='2')
			 {
			    $sort[$key]['type1']='优惠卷';
			 }else
			 {
				 $sort[$key]['type1']='道具';
			 }
        }
	 }
	 $smarty->assign('sort_list',$sort);
	 $smarty->display(ADMIN_TPL.'usual_sort_list.dwt');

}
?>